home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / SAFEIO.C < prev    next >
C/C++ Source or Header  |  1993-04-06  |  6KB  |  200 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s a f e i o . c                                                 */
  3. /*                                                                    */
  4. /*    Console I/O functions for use during interrupt processing       */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Since C I/O functions are not safe inside signal routines,      */
  9. /*    the code uses conditionals to use system-level DOS and OS/2     */
  10. /*    services.  Another option is to set global flags and do any     */
  11. /*    I/O operations outside the signal handler.                      */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. #define __MSC                 /* Make Borland C++ 2.0 act like MS C  */
  15.  
  16. #include <stdio.h>
  17.  
  18. #if defined( WIN32 )
  19.     #include <windows.h>
  20.     #include <string.h>
  21. #else /* WIN32 */
  22. #if defined( FAMILYAPI )
  23.     #define INCL_NOCOMMON
  24.     #define INCL_NOPM
  25.     #define INCL_VIO
  26.     #define INCL_KBD
  27.     #include <os2.h>
  28.     #include <string.h>
  29. #else /* FAMILYAPI */
  30.     #include <conio.h>
  31.     #include <dos.h>
  32.     #include <bios.h>
  33. #endif
  34. #endif
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                    UUPC/extended include files                     */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include "lib.h"
  41. #include "safeio.h"
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*                    Local Structures and defines                    */
  45. /*--------------------------------------------------------------------*/
  46.  
  47. #ifdef FAMILYAPI
  48. /* KBDKEYINFO structure, for KbdCharIn and KbdPeek */
  49.  
  50. #ifdef __GNUC__
  51. typedef struct _KBDKEYINFO {  /* kbci */
  52.    UCHAR chChar;
  53.    UCHAR chScan;
  54.    UCHAR fbStatus;
  55.    UCHAR bNlsShift;
  56.    USHORT   fsState;
  57.    ULONG time;
  58. }KBDKEYINFO;
  59.  
  60. #ifndef IO_WAIT
  61. #define IO_WAIT      0
  62. #endif
  63.  
  64. #ifndef FINAL_CHAR_IN
  65. #define FINAL_CHAR_IN       0x40
  66. #endif
  67.  
  68. #endif
  69. #endif
  70.  
  71. /*--------------------------------------------------------------------*/
  72. /*    s a f e i n                                                     */
  73. /*                                                                    */
  74. /*    Inputs a character using system level calls.  From MicroSoft    */
  75. /*    Programmer's Workbench QuickHelp samples                        */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. int safein( void )
  79. {
  80. #ifdef _Windows
  81.    return getchar( );
  82. #else
  83. #if defined( WIN32 )
  84.    CHAR ch;
  85.    DWORD dwBytesRead;
  86.    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  87.  
  88.    ReadFile(hStdIn, &ch, 1, &dwBytesRead, NULL);      
  89.  
  90.    return ch;
  91. #else /* WIN32 */
  92. #if defined( FAMILYAPI )
  93.     KBDKEYINFO kki;
  94.  
  95.     KbdCharIn( &kki, IO_WAIT, 0 );
  96.     return kki.chChar;
  97. #else /* FAMILYAPI */
  98.  
  99.     int c = (_bios_keybrd( _KEYBRD_READ ) & 0xff );
  100.     union REGS inregs, outregs;
  101.  
  102.     inregs.h.ah = 0x0e;
  103.     inregs.h.al = (char) c;
  104.     int86( 0x10, &inregs, &outregs );
  105.     return c;
  106.  
  107. #endif /* FAMILYAPI */
  108. #endif /* WIN32 */
  109. #endif /* _Windows */
  110. } /* safein */
  111.  
  112. /*--------------------------------------------------------------------*/
  113. /*    s a f e p e e k                                                 */
  114. /*                                                                    */
  115. /*    Determine if a character is waiting at the keyboard for us.     */
  116. /*    Written by ahd based on safein (above).                         */
  117. /*--------------------------------------------------------------------*/
  118.  
  119. boolean safepeek( void )
  120. {
  121.  
  122. /*--------------------------------------------------------------------*/
  123. /*                         OS/2 keyboard peek                         */
  124. /*--------------------------------------------------------------------*/
  125. #ifdef _Windows
  126.    return 0;
  127. #else /* _Windows */
  128. #ifdef WIN32
  129.    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  130.    INPUT_RECORD Buffer;
  131.    DWORD nEventsRead;
  132.  
  133.    PeekConsoleInput(hStdIn, &Buffer, 1, &nEventsRead);
  134.  
  135.    if (nEventsRead != 0 && Buffer.EventType == KEY_EVENT)
  136.       return TRUE;
  137.    return FALSE;
  138. #else /* WIN32 */
  139. #if defined( FAMILYAPI )
  140.     KBDKEYINFO kki;
  141.  
  142.     KbdPeek( &kki, 0 );
  143.     return (kki.fbStatus & FINAL_CHAR_IN);
  144. #else /* FAMILYAPI */
  145.  
  146. /*--------------------------------------------------------------------*/
  147. /*                         DOS Keyboard peek                          */
  148. /*--------------------------------------------------------------------*/
  149.  
  150.     return (_bios_keybrd( _KEYBRD_READY ) & 0xff );
  151. #endif /* FAMILYAPI */
  152. #endif /* WIN32 */
  153. #endif /* _Windows */
  154.  
  155. } /* safepeek */
  156.  
  157. /*--------------------------------------------------------------------*/
  158. /*    s a f e f l u s h                                               */
  159. /*                                                                    */
  160. /*    Flush the keyboard look ahead buffer.                           */
  161. /*    Written by ahd based on safein (above).                         */
  162. /*--------------------------------------------------------------------*/
  163.  
  164. void safeflush( void )
  165. {
  166.  
  167. #ifdef _Windows
  168.    return;
  169. #else
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*                         OS/2 keyboard flush                        */
  173. /*--------------------------------------------------------------------*/
  174.  
  175. #ifdef WIN32
  176.    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  177.    FlushConsoleInputBuffer(hStdIn);
  178. #else
  179. #if defined( FAMILYAPI )
  180.     KbdFlushBuffer( 0 );      /* That's all!  (Makes you love rich
  181.                                  API's, doesn't it?)                 */
  182.  
  183. #else
  184.  
  185. /*--------------------------------------------------------------------*/
  186. /*                         DOS Keyboard flush                         */
  187. /*--------------------------------------------------------------------*/
  188.  
  189.    union REGS regs;
  190.  
  191.    regs.h.ah = 0x0C;       /* Flush buffer, read keyboard            */
  192.    regs.h.al = 0x00;       /* Don't actually read keyboard           */
  193.    intdos( ®s, ®s ); /* Make it happen                         */
  194.  
  195. #endif /* FAMILYAPI */
  196. #endif /* WIN32 */
  197. #endif /* _Windows */
  198.  
  199. } /* safeflush */
  200.